'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 充值商品配置
 * @author sundbcn@gmail.com (sun debing)
 */

var dataApi = require('../libs/dataApi');

/**
 * 获取配置
 * @param  {[type]} product_id [description]
 * @return {[type]}            [description]
 */
exports.getConf = function (product_id) {
    return _getConf(product_id);
};

/**
 * 获取vip等级配置
 * @param  {[type]} vip [description]
 * @return {[type]}     [description]
 */
exports.getProductDiamond = function (product_id, is_first) {
    var pay_product_conf = _getConf(product_id);
    if (!pay_product_conf) {
        console.error('pay_product_conf type can not found:' + product_id);
        return 0;
    }

    if (!!is_first) {
        return pay_product_conf.point + pay_product_conf.rebate_num;
    } else {
        return pay_product_conf.point;
    }
};

/**
 * 获取月卡的天数
 * @param  {[type]} product_id [description]
 * @return {[type]}            [description]
 */
exports.getProductMonthDay = function (product_id) {
    var pay_product_conf = _getConf(product_id);
    if (!pay_product_conf) {
        console.error('pay_product_conf type can not found:' + product_id);
        return 0;
    }

    return pay_product_conf.rebate_times;
};

var _getConf = function (product_id) {
    return dataApi.pay_product.findById(product_id);
};

/**
 * 查找累计充值奖励
 * @param  {[type]} price [description]
 * @return {[type]}     [description]
 */
exports.getTotalPayConf = function (price) {
    return dataApi.cumulate_pay.findById(price);
};

/**
 * 按金额查找每日累计充值奖励
 * @param  {[type]} price [description]
 * @return {[type]}     [description]
 */
exports.getDailyPayConf = function (price) {
    return dataApi.daily_pay.findById(price);
};

/**
 * 查找累计充值天数奖励
 * @param  {[type]} day [description]
 * @return {[type]}     [description]
 */
exports.getTotalPayDayConf = function (day) {
    return dataApi.cumulate_day.findById(day);
};

/**
 * 查询下一档单笔充值配置
 * @param  {[type]} product_id [description]
 * @return {[type]}     [description]
 */
exports.getNextSinglePayConf = function (greater_than) {
    var single_pay_conf = null;
    if (greater_than < 10000000) {
        single_pay_conf = dataApi.one_time_box_price.find(function (item) {
            return item.id > greater_than;
        });
    } else {
        greater_than -= 10000000;
    }

    if (!single_pay_conf) {
        single_pay_conf = dataApi.one_time_box_price.find(function (item) {
            return item.id === greater_than;
        });
        if (!!single_pay_conf) {
            single_pay_conf.id += 10000000; //大于1000万表示没有下一档
        }
    }

    return single_pay_conf;
};

/**
 * 查询下一档单笔充值奖励信息
 * @param  {[type]} product_id [description]
 * @return {[type]}     [description]
 */
exports.getSinglePayReward = function (price) {
    price = +price;
    var reward = dataApi.one_time_box.find(function (item, key) {
        return +key === price;
    });
    return reward;
};

/**
 * 查询下一档单笔充值配置信息
 * @param  {[type]} product_id [description]
 * @return {[type]}     [description]
 */
exports.getSinglePayConf = function (price) {
    price = +price;
    return dataApi.one_time_box_price.findById(price);
};

/**
 * 查询首充礼包
 * @param  {[type]} product_id [description]
 * @return {[type]}     [description]
 */
exports.getFirstPayConf = function (product_id) {
    var first_pay_conf = dataApi.first_pay.findById(product_id);
    // todo: 判断 奖励是否为数组，如果不是，则转成数组

    return first_pay_conf;
};
